/**
 * Copyright 2012 Roger Cockshutt
 * krakenbrau@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.parallelrl.util;

/**
 * @author Roger Cockshutt
 *
 */
public class Point2D {
	
	public static final Point2D up = new Point2D(-1, 0);
	public static final Point2D down = new Point2D(1, 0);
	public static final Point2D left = new Point2D(0, -1);
	public static final Point2D right = new Point2D(0, 1);
	public static final Point2D zero = new Point2D(0, 0);
	
	public Integer x;
	public Integer y;

	public Point2D(Integer yVal, Integer xVal) {
		x = xVal;
		y = yVal;
	}
	
	@Override
	public String toString() {
		return y + ", " + x;
	}
	
	@Override
	public boolean equals(Object o) {
		if (o instanceof Point2D) {
			Point2D p = (Point2D) o;
			return (y.equals(p.y) && x.equals(p.x));
		}
		return false;
	}
	
	public static Point2D valueOf(String s) {
		Integer y, x;
		y = null;
		x = null;
		String[] splitPos = s.split(",");
		if (splitPos.length >= 2) {
			y = Integer.valueOf(splitPos[0].trim());
			x = Integer.valueOf(splitPos[1].trim());
			
		}
		if (!(y == null || x == null)) {
			return new Point2D(y, x);
		}
		return null;
	}
	
	public Point2D sum(Point2D p) {
		return new Point2D(y + p.y, x + p.x);
	}
	
	public static Point2D randomDirection() {
		int y = MersenneTwister.getNext(3) - 1;
		int x = MersenneTwister.getNext(3) - 1;
		return new Point2D(y, x);
	}
	
	public Double distanceTo(Point2D pos2) {
		Double dist = null;
		if (pos2 != null) {
			Double yDist = (double) (pos2.y - y);
			Double xDist = (double) (pos2.x - x);
			dist = Math.sqrt(Math.pow(yDist, 2) + Math.pow(xDist, 2));
		}
		return dist;
	}

}
